package com.dalei.vds.service;

import com.dalei.vds.environment.SystemStatus;
import com.dalei.vds.soap.SessionSoapImpl;
import com.dalei.vdserver.shared.soap.SessionSoap;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class SessionService extends Service {

	public static final double RATIO = 0.8;

	private SessionSoap sessionSoap;

	private boolean isRunning;

	private Thread requestSessionThread;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		sessionSoap = new SessionSoapImpl(this);

		if (SystemStatus.getDeviceId() > 0 && SystemStatus.getUserId() > 0) {
			isRunning = true;
		}

		requestSessionThread = new RequestSessionThread();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		if (!requestSessionThread.isAlive()) {
			requestSessionThread.start();
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		isRunning = false;
	}

	private class RequestSessionThread extends Thread {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();

			int deviceId = SystemStatus.getDeviceId();
			int userId = SystemStatus.getUserId();

			while (isRunning) {
				long sessionTimeout = sessionSoap.requesSession(deviceId,
						userId);
				if (sessionTimeout > 0) {
					SystemStatus.setSessionTimeout(sessionTimeout);
					try {
						Thread.sleep((long) (sessionTimeout * RATIO));
						Log.i("SessionService", "Request session successful!");
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					int i;
					for (i = 0; i < 5; i++) {
						SystemStatus.setSessionTimeout(sessionTimeout);

						switch (i) {
						case 0:
							Log.i("SessionService",
									"Retry to request the session for the first time!");
							break;

						case 1:
							Log.i("SessionService",
									"Retry to request the session for the second time!");
							break;

						case 2:
							Log.i("SessionService",
									"Retry to request the session for the third time!");
							break;

						case 3:
							Log.i("SessionService",
									"Retry to request the session for the fourth time!");
							break;

						case 4:
							Log.i("SessionService",
									"Retry to request the session for the fifth time!");
							break;

						default:
							break;
						}

						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

					if (i == 5) {
						isRunning = false;
						SystemStatus.setLoggedOn(false);
					}
				}
			}
		}
	}
}
